Module (chicken number-vector)
Homogeneous numeric vector datatypes. This module provides a superset of SRFI-4. The module srfi-4 is also available for compatibility reasons.
When loaded, the feature identifier srfi-4 is defined.
CHICKEN implementation specifics and extensions to SRFI-4
- Procedures for bytevector conversion, subvectors and vector I/O are provided.
- SRFI-17 setters for
XXXvector-refare defined. - Constructors allow allocating the storage in non garbage collected memory.
- Vectors for 64 and 128-bit complex numbers are provided.
This module provides a set of datatypes for vectors whose elements are of the same numeric type (signed or unsigned exact integer or inexact real of a given precision). These datatypes support operations analogous to the Scheme vector type, but they are distinct datatypes. An external representation is specified which must be supported by the read and write procedures and by the program parser (i.e. programs can contain references to literal homogeneous vectors).
Datatypes
There are 8 datatypes of exact integer homogeneous vectors (which will be called integer vectors):
| Datatype | Type of elements |
|---|---|
s8vector | signed exact integer in the range -(2^7) to (2^7)-1 |
u8vector | unsigned exact integer in the range 0 to (2^8)-1 |
s16vector | signed exact integer in the range -(2^15) to (2^15)-1 |
u16vector | unsigned exact integer in the range 0 to (2^16)-1 |
s32vector | signed exact integer in the range -(2^31) to (2^31)-1 |
u32vector | unsigned exact integer in the range 0 to (2^32)-1 |
s64vector | signed exact integer in the range -(2^31) to (2^31)-1 |
u64vector | unsigned exact integer in the range 0 to (2^64)-1 |
s64vector | signed exact integer in the range -(2^63) to (2^63)-1 |
u64vector | unsigned exact integer in the range 0 to (2^64)-1 |
There are 2 datatypes of inexact real homogeneous vectors (which will be called float vectors):
| Datatype | Type of elements |
|---|---|
f32vector | inexact real |
f64vector | inexact real |
The only difference between the two float vector types is that f64vectors preserve at least as much precision as f32vectors.
And there are two datatypes of inexact complex homogeneous vectors (which will be called complex vectors):
| Datatype | Type of elements |
|---|---|
c64vector | inexact complex, typically 64 bits |
c128vector | inexact complex, typically 128 bits |
Each homogeneous vector datatype has an external representation which is supported by the read and write procedures and by the program parser. Each datatype also has a set of associated predefined procedures analogous to those available for Scheme's heterogeneous vectors.
External representation
The external representation of instances of the datatype XXXvector is #XXX( ...elements... ).
For example,
#u8(0 #e1e2 #xff)}} ; a {{u8vector}} of length 3 containing 0, 100, 255
#f64(-1.5) ; a {{f64vector}} of length 1 containing -1.5.This external representation is also available in program source code. For example,
(set! x '#u8(1 2 3))
will set x to the object #u8(1 2 3). Since CHICKEN 4.9.0, literal homogeneous vectors do not have to be quoted. Homogeneous vectors can appear in quasiquotations but must not contain unquote or unquote-splicing forms. I.e.,
`(,x #u8(1 2)) ; legal `#u8(1 ,x 2) ; illegal
Elements may also be characters or strings, in that case they are interpreted as a sequence of numerical character codes. For example,
'#u8(#\x7f "EL" #\F 2 1)
is equivalent to
'#u8(#\x7f #\x45 #\x4c #\x46 2 1)
Character literals inside numeric vectors expand into the UTF-8 sequence of the characters they represent, for strings the contained characters are interpreted in whatever encoding is used for the text file or stream in which the literal appears.
Note that #u8"..." can be used as an abbreviation for the special case #u8("...").
Predicates
Return #t if obj is an object of the specified type or #f if not.
(number-vector? OBJ)Return #t if obj is a number vector, #f if not. A "number vector" is any of the homogeneous number vector types defined by SRFI-4, ie it's one of u8vector, s8vector, u16vector, s16vector, u32vector, s32vector, u64vector, s64vector, f32vector, f64vector, c64vector or c128vector.
Constructors
Return a newly-allocated SRFI-4 homogeneous number vector of length N.
If the optional fill VALUE is specified, it specifies the initial value for each slot in the vector. If not, the content of the vector is unspecified but individual elements of the vector are guaranteed to be in the range of values permitted for that type of vector.
The type of the fill value must be compatible with the elements of the vector datatype. It is an error if otherwise -- for example, if an inexact integer is passed to make-u8vector.
On CHICKEN, these procedures have been extended to allow allocating the storage in non-garbage collected memory, as follows:
The optional arguments NONGC and FINALIZE define whether the vector should be allocated in a memory area not subject to garbage collection and whether the associated storage should be automatically freed (using finalization) when there are no references from Scheme variables and data. NONGC defaults to #f (the vector will be located in normal garbage collected memory) and FINALIZE defaults to #t. Note that the FINALIZE argument is only used when NONGC is true.
Return a newly-allocated SRFI-4 homogeneous number vector of the specified type, composed of the arguments.
Length
Returns the length of the SRFI-4 homogeneous number VECTOR.
Getters
Return the value of the ith element of the SRFI-4 homogeneous number vector, where I is a nonnegative exact integer less than the length of the vector.
Setters
Set the ith element of the SRFI-4 homogeneous number VECTOR to VALUE. I is a nonnegative exact integer less than the length of the vector and VALUE must be the same type as the elements of the vector datatype.
Additionally, SRFI-17 setters are defined on all xxxvector-ref procedures. For example, to set the ith element of SRFI-4 u8vector to u8value:
(set! (u8vector-ref u8vector i) u8value)
Conversions
Return a list consisting of the elements of SRFI-4 homogeneous number VECTOR.
Return a newly-allocated SRFI-4 homogeneous number VECTOR consisting of the elements of LIST. Each element of LIST must be compatible with the datatype of VECTOR.
Blob conversions
As a number vector is basically just a bytevector wrapped into a record type, there are several procedures which can convert between bytevectors and number vectors.
Note that built-in bytevectors are identical to u8vectors.
Each of these procedures return the contents of the given vector as a 'packed' bytevector. The byte order in that vector is platform-dependent (for example little-endian on an Intel processor). The /shared variants return a bytevector that shares memory with the contents of the vector, the others will copy the contents of the vector's internal bytevector object.
Each of these procedures return a vector where the argument BYTEVECTOR is taken as a 'packed' representation of the contents of the vector. The /shared variants return a vector that shares memory with the contents of the bytevector, the others will copy the bytevector.
Subvectors
Creates a fresh number vector of the same type as the argument vector with the elements at the positions FROM up to but not including TO.
Release number vectors allocated in static memory
(release-number-vector NVECTOR)Release the storage of a SRFI-4 vector that was allocated in non-garbage collected memory (for example using the NONGC argument for one of the make-XXXvector constructor procedures). The effect of calling this procedure with a number vector allocated in normal garbage collected memory is undefined.
Previous: Module (chicken type)